﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebApplication2.Baga
{
    public class Sqlserver
    {
        private string strs = "server=LAPTOP-8HS8J7NL;uid=sa;pwd=2287431003a;database=Student_db";
        private SqlConnection con = null;

        public Sqlserver() {
            con = new SqlConnection(strs);
        }
        public DataTable Get(string sql, SqlParameter[] parameters)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand command = new SqlCommand(sql, con);
                if (parameters != null && parameters.Length != 0)
                {
                    command.Parameters.AddRange(parameters);
                }
                //SqlDataReader reader = command.ExecuteReader();
                //if (reader.Read())
                //{                
                //    Response.Redirect("sessend.aspx");
                //}		HelpLink	null	string
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet); 

                return dataSet.Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
           }

        public bool Extern(string sql, SqlParameter[] parameters)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand command = new SqlCommand(sql, con);
                if (parameters != null && parameters.Length != 0)
                {
                    command.Parameters.AddRange(parameters);
                }
                return command.ExecuteNonQuery()>0;            
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }





    }
}